package com.tpv.app.volley.demo.util;

import android.text.TextUtils;

import com.android.volley.VolleyLog;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class BodyContentImpl implements IBodyContent
{
    private String contentType = "multipart/form-data";
    private String boundary = "--boundary-2018-12-21-upload-file";
    
    @Override
    public String getBodyStartBoundary()
    {
        return "--" + boundary;
    }
    
    @Override
    public String getBodyBoundary()
    {
        return "--" + boundary;
    }
    
    @Override
    public String getBodyEndBoundary()
    {
        return "--" + boundary + "--";
    }
    
    @Override
    public String getContentDisposition()
    {
        return "Content-Disposition:form-data";
    }
    
    @Override
    public String getName(String name)
    {
        return "name=\"" + name + "\"";
    }
    
    @Override
    public Object getValue(Object value)
    {
        return value;
    }
    
    @Override
    public String getFileName(String fileName)
    {
        return "filename=\"" + fileName + "\"";
    }
    
    @Override
    public String getFileName(File file)
    {
        return "filename=\"" + file.getName() + "\"";
    }
    
    @Override
    public String getContentType(String contentType)
    {
        return "Content-Type:" + contentType;
    }
    
    @Override
    public String getLinesFeed()
    {
        return "\r\n";
    }
    
    @Override
    public String getSemicolon()
    {
        return ";";
    }
    
    @Override
    public String getBodyContentType()
    {
        return contentType + getSemicolon() + "boundary=" + boundary;
    }
    
    @Override
    public String getFileSuffixName(File file)
    {
        String fileName = file.getName();
        String suffixName;
        boolean contains = fileName.contains(".");
        if (contains)
        {
            String[] split = fileName.split("\\.");
            if (split.length > 1)
            {
                suffixName = split[1];
            } else
            {
                suffixName = "*/*";
            }
        } else
        {
            suffixName = "*/*";
        }
        return suffixName;
    }
    
    @Override
    public String getFileMimeType(String suffix)
    {
        suffix = suffix.toLowerCase();
        String mimeType;
        switch (suffix)
        {
            case "png":
                mimeType = "image/png";
                break;
            case "jpeg":
                mimeType = "image/jpeg";
                break;
            case "jpg":
                mimeType = "image/jpg";
                break;
            default:
                mimeType = "*/*";
                break;
            
        }
        return mimeType;
    }
    
    @Override
    public String getFileMimeType(File file)
    {
        String suffix =getFileSuffixName(file);
        return getFileMimeType(suffix);
    }
    
    @Override
    public void writeParams(ByteArrayOutputStream bos, String params) throws IOException
    {
        bos.write(params.getBytes("UTF-8"));
    }
    
    @Override
    public void writeFile(ByteArrayOutputStream bos, File file) throws IOException
    {
        FileInputStream inputStream = new FileInputStream(file);
        final byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1)
        {
            VolleyLog.e("len --> %s", String.valueOf(len));
            bos.write(buffer, 0, len);
        }
    }
    
    @Override
    public void writeEndBoundary(ByteArrayOutputStream bos) throws IOException
    {
        bos.write(getLinesFeed().getBytes("UTF-8"));
        bos.write(getLinesFeed().getBytes("UTF-8"));
        bos.write(getBodyEndBoundary().getBytes("UTF-8"));
        bos.write(getLinesFeed().getBytes("UTF-8"));
    }
    
}
